package com.jeesite.modules.dataScreen.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * 大屏api配置
 *
 * @author lh
 * @version 2023年5月10日
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(
        name = {"web.swagger.enabled"},
        havingValue = "true",
        matchIfMissing = true
)
@EnableSwagger2WebMvc
public class DataScreenApiConfig implements WebMvcConfigurer {

    // @Override
    // public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //     registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    //     registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    //     registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    // }

    public DataScreenApiConfig() {
    }

    @Bean
    @ConditionalOnProperty(
            name = {"web.swagger.buss.enabled"},
            havingValue = "true",
            matchIfMissing = true
    )
    public Docket dataScreenBussApi() {
        String moduleCode  = "103-dataScreenApi";
        String moduleName  = "大屏接口模块";
        String basePackage = "com.jeesite.modules.dataScreen.web";
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("jeecg")
                .apiInfo(apiInfo())
                .select()
                // 此包路径下的类，才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("com.jeesite.modules.dataScreen.web"))
                // 加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
        // .securitySchemes(Collections.singletonList(securityScheme()))
        // .securityContexts(securityContexts());
        //.globalOperationParameters(setHeaderToken());
        // return SwaggerConfig.docket(moduleCode, moduleName, basePackage).select().apis(RequestHandlerSelectors.any().and(RequestHandlerSelectors.basePackage(basePackage))).build();
    }

    /**
     * api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // //大标题
                .title("Jeecg-Boot 后台服务API接口文档")
                // 版本号
                .version("1.0")
//				.termsOfServiceUrl("NO terms of service")
                // 描述
                .description("后台API接口")
                // 作者
                .contact(new Contact("北京国炬信息技术有限公司", "www.jeccg.com", "jeecgos@163.com"))
                // .contact("JEECG团队")
                .license("The Apache License, Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }
}
